import requests
import re


class CSBK():
    def __init__(self):
        self.url ='https://www.qiushibaike.com/hot/page/'
        self.Index = 1
        self.user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) ' \
                          'AppleWebKit/537.36 (KHTML, like Gecko) ' \
                          'Chrome/63.0.3239.132 Safari/537.36'
        self.headers = {'User-Agent': self.user_agent}
        self.enable = True
        self.stories_list = []

    def get_page(self):
        url = self.url + str(self.Index) + "/"
        try:
            resp = requests.get(url, headers=self.headers, timeout=5)
            if resp.status_code == 200:
                self.Index += 1
                return resp.text
        except Exception as e:
            if hasattr(e,'reason'):
                print('open web failure:\n', e.reason)

    def get_page_items(self):
        page_content = self.get_page()
        if not page_content:
            print('load web content failure!')

        regx = re.compile(r'<div class="author clearfix">.*?alt="(.*?)">.*?<div class="content">\n<span>(.*?)</span>',
                          re.S)
        Items = re.findall(regx, page_content)
        return Items

    def start(self):
        self.stories_list.extend(self.get_page_items())
        for story in enumerate(self.stories_list):
            print(story[0], ".", story[1][0], ":\n", story[1][1].strip())
            print("="*60)
            

if __name__ == "__main__":
    first = CSBK()
    first.start()
